package com.dianban.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.*;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.BankCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.BankCardOCRResponse;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Base64;

/**
 * 腾讯云银联四要素
 * https://console.cloud.tencent.com/api/explorer?Product=faceid&Version=2018-03-01&Action=BankCard4EVerification&SignVersion=
 * 直接在上面拷贝例子
 */
public class TencentRealnameAuthUtils {
    public static Boolean bankCard4EVerification(String name, String bankCard, String phone, String idcard) {
        Boolean result = false;
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(TencentUtils.SECRET_ID, TencentUtils.SECRET_KEY);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("faceid.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            FaceidClient client = new FaceidClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            BankCard4EVerificationRequest req = new BankCard4EVerificationRequest();
            req.setName(name);
            req.setBankCard(bankCard);
            req.setPhone(phone);
            req.setIdCard(idcard);
            // 返回的resp是一个BankCard4EVerificationResponse的实例，与请求对象对应
            BankCard4EVerificationResponse resp = client.BankCard4EVerification(req);
            if (resp != null && resp.getResult().equals("0")) {
                result = true;
            }
            // 输出json格式的字符串回包
            System.out.println(BankCard4EVerificationResponse.toJsonString(resp));
            // {"Result":"0","Description":"认证通过","RequestId":"f8ede9d1-6fce-4c81-8e6b-0631322cb2c8"}
            // {"Result":"-1","Description":"认证未通过","RequestId":"4aeefb05-4aee-4bfc-b843-27261aaba5ce"}
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return result;
    }

    public static IDCardOCRResponse idcardOcr(String adminDomain, String imageUrl) {
        try {
            // 解决路径带中文问题
            imageUrl = URLEncoder.encode(adminDomain + imageUrl, "UTF-8");
            imageUrl = imageUrl.replaceAll("%2F", "/");
            imageUrl = imageUrl.replaceAll("%3A", ":");
            imageUrl = imageUrl.replaceAll("\\+", "%20");
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(TencentUtils.SECRET_ID, TencentUtils.SECRET_KEY);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            OcrClient client = new OcrClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            IDCardOCRRequest req = new IDCardOCRRequest();
            req.setImageUrl(imageUrl);
            // 不传自动判断正反面
            //req.setCardSide("FRONT");
            // 返回的resp是一个IDCardOCRResponse的实例，与请求对象对应
            IDCardOCRResponse resp = client.IDCardOCR(req);
            // 输出json格式的字符串回包
            System.out.println(IDCardOCRResponse.toJsonString(resp));
            return resp;
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static BankCardOCRResponse bankcardOcr(String adminDomain, String imageUrl) {
        try {
            // 解决路径带中文问题
            imageUrl = URLEncoder.encode(adminDomain + imageUrl, "UTF-8");
            imageUrl = imageUrl.replaceAll("%2F", "/");
            imageUrl = imageUrl.replaceAll("%3A", ":");
            imageUrl = imageUrl.replaceAll("\\+", "%20");
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(TencentUtils.SECRET_ID, TencentUtils.SECRET_KEY);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            OcrClient client = new OcrClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            BankCardOCRRequest req = new BankCardOCRRequest();
            req.setImageUrl(imageUrl);
            // 返回的resp是一个BankCardOCRResponse的实例，与请求对象对应
            BankCardOCRResponse resp = client.BankCardOCR(req);
            // 输出json格式的字符串回包
            System.out.println(BankCardOCRResponse.toJsonString(resp));
            return resp;
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 获取文件的base64
    public static String getBase64Str(String fileUrl) {
        String result = "";
        try {
            InputStream inputStream = FileUtils.openInputStream(new File(fileUrl));
            // encode
            result = new String(Base64.getEncoder().encode(IOUtils.toByteArray(inputStream)), Charsets.UTF_8);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return result;
    }

    // 数字的活体检测
    public static Boolean liveCheck(String videoBase64, String validateData) {
        Boolean result = false;
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(TencentUtils.SECRET_ID, TencentUtils.SECRET_KEY);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("faceid.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            FaceidClient client = new FaceidClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            LivenessRequest req = new LivenessRequest();
            req.setVideoBase64(videoBase64);
            req.setLivenessType("LIP");
            req.setValidateData(validateData);
            // 返回的resp是一个LivenessResponse的实例，与请求对象对应
            LivenessResponse resp = client.Liveness(req);
            // 输出json格式的字符串回包
            System.out.println(LivenessResponse.toJsonString(resp));
            if (resp != null && resp.getResult().equalsIgnoreCase("Success")) {
                result = true;
            }
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return result;
    }

    // 获取活体检测数字验证码
    public static String getLiveCode() {
        String liveCode = "";
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(TencentUtils.SECRET_ID, TencentUtils.SECRET_KEY);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("faceid.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            FaceidClient client = new FaceidClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            GetLiveCodeRequest req = new GetLiveCodeRequest();

            // 返回的resp是一个GetLiveCodeResponse的实例，与请求对象对应
            GetLiveCodeResponse resp = client.GetLiveCode(req);
            // 输出json格式的字符串回包
            System.out.println(GetLiveCodeResponse.toJsonString(resp));
            if (resp != null) {
                liveCode = resp.getLiveCode();
            }
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return liveCode;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        //bankCard4EVerification("田红梅", "6214837554188833", "13600142693", "231027197711030026");
//        String str = URLEncoder.encode("http://yun.dianbang-st.com/uploads/idcard/田红梅+231027197711030026+national.png", "UTF-8");
//        str = str.replaceAll("%2F","/");
//        str = str.replaceAll("%3A",":");
//        str = str.replaceAll("\\+","%20");
//        System.out.println(str);
//        idcardOcr(str);
        //idcardOcr("http://yun.dianbang-st.com", "/uploads/idcard/田红梅+231027197711030026+national.png");
        //idcardOcr("http://yun.dianbang-st.com", "/uploads/idcard/田红梅+231027197711030026+portrait.png");
        bankcardOcr("https://inews.gtimg.com", "/newsapp_bt/0/13770567688/641");
        //liveCheck(getBase64Str("/Users/Desktop/1642127214027365.mp4"), "1111");//2369
    }
}
